﻿using System;
using System.Configuration;

namespace CodeCamp.Infrastructure
{
    public class DefaultValueProvider : IDefaultValueProvider
    {
        public DefaultValueProvider()
        {
        }

        public string GetDefaultStringValue(string valueName, string defaultValue)
        {
            return GetStringValue(valueName, defaultValue, false);
        }

        public string GetStringValue(string valueName, string defaultValue)
        {
            return GetStringValue(valueName, defaultValue, false);
        }

        public string GetStringValueOrNull(string valueName)
        {
            return GetStringValue(valueName, null, true);
        }

        public string GetStringValue(string valueName, string defaultValue, bool allowNull)
        {
            if (string.IsNullOrWhiteSpace(valueName))
                throw new ArgumentException("valueName is null or empty.", "valueName");

            string defaultStrValue = ConfigurationManager.AppSettings[valueName];
            if (!allowNull && string.IsNullOrWhiteSpace(defaultStrValue))
            {
                if (string.IsNullOrWhiteSpace(defaultValue))
                    throw new ArgumentException(string.Format("No value found for key \"{0}\" and defaultValue is null or empty.",
                        valueName)
                        , "defaultValue");

                defaultStrValue = defaultValue;
            }
            return defaultStrValue;
        }
    }
}